package com.warrior.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.warrior.entity.UserEntity;
import com.warrior.service.UserService;
import com.warrior.util.CModelAndView;
import com.warrior.util.CommonUtils;
import com.warrior.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by shawn on 2017/4/26.
 */
@Controller
@RequestMapping("/admin")
public class AdminUserController {

    @Autowired
    private UserService userService;

    /**
     * 列出所有用户
     * @param request req
     * @param response resp
     * @param page 当前页
     * @param size 页码
     * @param orderType 排序类型
     * @return
     */
    @RequestMapping("/list_all_user")
    public ModelAndView listAllUsers(HttpServletRequest request, HttpServletResponse response, String page, String size, String orderType) {
        ModelAndView mv = new CModelAndView("admin/user_list", request, response);
        Page<UserEntity> allUsers = this.userService.findAllUsers(CommonUtils.String2Int(size),CommonUtils.String2Int(page),orderType);

        mv.addObject("users", allUsers);
        return mv;

    }


    /**
     * 修改用户状态
     * @param userId 用户id
     * @param userStatus 需要修改的状态,记录用户的状态 0表示未激活
     * @return
     */
    @RequestMapping("modified_user_status")
    @ResponseBody
    public String modifiedUserStatus(String userId,String userStatus){
        UserEntity userEntity = this.userService.findUserById(userId);
        userEntity.setUserStatus(CommonUtils.String2Int(userStatus));
        this.userService.saveUser(userEntity);
        String returnMap = null;
        try {
            returnMap= ServerResponse.okToJsonString("success");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return returnMap;
    }

    /**
     * 用户详细信息
     * @param userId 用户id
     * @param request req
     * @param response resp
     * @return
     */
    @RequestMapping("user_details")
    public ModelAndView showUserDetails(String userId,HttpServletRequest request, HttpServletResponse response){
        ModelAndView mv = new CModelAndView("/admin/user_edit",request,response);
        UserEntity userEntity = this.userService.findUserById(userId);
        mv.addObject("user",userEntity);
        return mv;
    }

    @RequestMapping("/modify_user_name")
    @ResponseBody
    public String modifyUserName(String userName,String userId){
        UserEntity userEntity = this.userService.findUserById(userId);
        userEntity.setUserName(userName);
        this.userService.saveUser(userEntity);
        String returnMap = null;
        try {
            returnMap= ServerResponse.okToJsonString("success");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return returnMap;
    }

    /**
     * 修改用户信息
     * @param userName 用户名
     * @param nickName 昵称
     * @param userId 用户id
     * @param userStatus 用户状态
     * @return String
     */
    @RequestMapping("/modify_user")
    @ResponseBody
    public String modifyUser(String userName,String nickName,String userId,String userStatus){
        UserEntity userEntity = this.userService.findUserById(userId);
        userEntity.setUserName(userName);
        userEntity.setUserStatus(CommonUtils.String2Int(userStatus));
        userEntity.setNickname(nickName);
        this.userService.saveUser(userEntity);
        String returnMap = null;
        try {
            returnMap= ServerResponse.okToJsonString("success");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return returnMap;
    }





}
